package controller;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import model.GroupManager;
import model.Project;
import model.Student;

import java.util.List;

@WebServlet("/DetailServlet")
public class DetailServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String groupNumberStr = request.getParameter("groupNumber");
        if (groupNumberStr == null) {
            response.sendRedirect("GroupServlet");
            return;
        }

        int groupNumber = Integer.parseInt(groupNumberStr);

        GroupManager groupManager = (GroupManager) getServletContext().getAttribute("groupManager");
        List<Project> projectList = (List<Project>) getServletContext().getAttribute("projectList");

        // 获取对应的项目
        Project project = null;
        if (groupNumber >= 1 && groupNumber <= projectList.size()) {
            project = projectList.get(groupNumber - 1);
        } else {
            response.sendRedirect("GroupServlet");
            return;
        }

        // 获取对应的组员
        List<Student> groupMembers = groupManager.getGroups().get(groupNumber);

        request.setAttribute("project", project);
        request.setAttribute("groupMembers", groupMembers);
        request.setAttribute("groupNumber", groupNumber);

        request.getRequestDispatcher("detail.jsp").forward(request, response);
    }
}
